<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class OrderController extends Controller
{
    // 订单列表
    public function index(Request $request){
        $data= DB::table('t_order as o')
            ->Join('users as u','u.id','=','o.uid') 
            ->Join('t_goods as g','g.gid','=','o.gid') 
            ->select('o.*','u.name as uname','g.gname')
            ->where('g.gid', 'like', '%'.$request -> keywords.'%') 
            -> paginate($request -> input('num', 10));
// dd($data);

            return view('admin.order.index',['title'=> '订单列表','data' => $data,'request' => $request -> all()]);
    }
    public function detail(Request $request,$order_num){
        $data = DB::table('t_order as o')
            ->Join('users as u','u.id','=','o.uid') 
            ->Join('t_goods as g','g.gid','=','o.gid') 
            ->select('o.*','u.name as uname','g.gname','g.gprice')
            ->where('o.order_num',$order_num)
            -> get();
            // dd($data);
        $totalPrice = 0;
        foreach ($data as $v) {
            $totalPrice+=($v->gprice *$v->count);
            $uname = $v->uname;
            $time = $v->add_time;
        }
        $list = ['order_num'=>$order_num,'uname'=>$uname,'totalPrice'=>$totalPrice,'time'=>$time];
        return view('admin.order.detail',['title'=>'订单详情','data'=>$data,'list'=>$list]);
    }
    public function ajax(Request $request){
        $order_status = DB::table('t_order')
            -> where('id',$request->id)
            -> value('order_status');
        $order_num = DB::table('t_order')
            -> where('id',$request->id)
            -> value('order_num');
        if($order_status=='0'){
            $data = DB::table('t_order')
                -> where('id',$request->id)
                -> update(['order_status'=>'1','shipping_status'=>'1']);
            return 1;
        }else{

            return $order_status;
        }
    }
    public function del(Request $request,$id){
        $data = DB::table('t_order')
            -> where('id',$id)
            -> delete();
            return back();
    }
}
